<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Configuration
 *
 * @author MinhNguyen
 */

class Reply {
    //put your code here
    public $id;
    public $statusid;
    public $userid;
    public $content;
    public $datecreated;
    public $user;

    public function __get($name) {
        return $this->$name;
    }
    
    public function __set($name, $value) {
        $this->$name = $value;
    }   

    /**
     * It's constuctor of User with email
     */
    
    public  function __construct() {
        //allocate your stuff
    }

    
    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $query = "SELECT * FROM replies WHERE id = '$id'";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $rows = $result->dataTable->rows[0];
                $instance->id = $rows["id"];
                $instance->statusid = $rows["statusid"];
                $instance->userid = $rows["userid"];
                $instance->content = $rows["content"];
                $instance->datecreated = $rows["datecreated"];
                $instance->user = User::withId($instance->userid);
                return $instance;
            }
            else {
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }  
    
    public static function withData($statusid, $userid, $content, $datecreated) {
        $instance = new self();
        $instance->statusid = $statusid;
        $instance->userid = $userid;
        $instance->content = $content;
        $instance->datecreated = $datecreated;
        return $instance;
    }
    
    public static function addReply($reply){
        $db = Database::getInstance();
        $id = $db->getID("replies", "id");
        $statusid = $reply->statusid;
        $userid = $reply->userid;
        $content = $reply->content;
        $datecreated = $reply->datecreated;
        $query = "INSERT INTO replies (id, statusid, userid, content, datecreated) VALUES 
                ('$id', '$statusid', '$userid', '$content', '$datecreated')";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            $status = Status::withId($reply->statusid);
            $value = $status->replies + 1;
            Status::updateColumn("replies", $value, $reply->statusid);
            return Reply::withId($id);
        }else
            return false;
    }
    
    public static function getReplies($statusid){
        $db = Database::getInstance();
        $query = "SELECT id FROM replies WHERE statusid = '$statusid'";
        $result = $db->query($query);
        if($result->returnCode == 200){
           if($result->dataTable->numOfRow > 0){
               $res = array();
               foreach ($result->dataTable->rows as $row){
                   $res[] = Reply::withId($row['id']);
               }
               return $res;
           }else
               return false;
        }else
            return false;
    }
    
    public static function getRepliesCount($statusid){
        $db = Database::getInstance();
        $query = "SELECT COUNT(id) FROM replies WHERE statusid = '$statusid'";
        $result = $db->query($query);
        if($result->returnCode == 200){
           if($result->dataTable->numOfRow == 1){
               $res = $result->dataTable->rows[0];
               return $res[0];
           }else
               return -1;
        }else
            return -1;
    }
}
?>
